package com.wtwd.campus.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wtwd.campus.dao.LastLocationDataMapper;
import com.wtwd.campus.entity.LastLocationData;
import com.wtwd.campus.service.LastLocationDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-12-01
 */
@Service
public class LastLocationDataServiceImpl extends ServiceImpl<LastLocationDataMapper, LastLocationData> implements LastLocationDataService {

    @Resource
    private LastLocationDataMapper lastLocationDataMapper;

    @Override
    public boolean InsertOrUpdate(LastLocationData lastLocationData) {
        if (lastLocationData == null || lastLocationData.getDeviceId() == null) {
            return false;
        }
        LastLocationData data = queryLastLocationDataByDeviceId(lastLocationData.getDeviceId());
        Integer rest = 0;
        if (data == null) {
            lastLocationData.setCreateTime(new Date().getTime());
            rest = lastLocationDataMapper.insert(lastLocationData);
        } else {
            lastLocationData.setId(data.getId());
            lastLocationData.setUpdateTime(new Date().getTime());
            rest = lastLocationDataMapper.updateById(lastLocationData);
        }
        return rest > 0 ? true : false;
    }

    @Override
    public LastLocationData queryLastLocationDataByDeviceId(Integer deviceId) {
        if (deviceId == null) {
            return null;
        }
        return new LambdaQueryChainWrapper<>(lastLocationDataMapper)
                .eq(LastLocationData::getDeviceId, deviceId)
                .one();
    }

    @Override
    public List<LastLocationData> queryLastLocationDataByDeviceIds(List<Integer> deviceIds) {
        if (deviceIds == null || deviceIds.size() == 0) {
            return null;
        }
        return new LambdaQueryChainWrapper<>(lastLocationDataMapper)
                .in(LastLocationData::getDeviceId, deviceIds)
                .list();

    }

    @Override
    public LastLocationData queryLastLocationDataByImei(String imei) {
        if (StringUtils.isBlank(imei)) {
            return null;
        }
        return new LambdaQueryChainWrapper<>(lastLocationDataMapper)
                .eq(LastLocationData::getDeviceImei, imei)
                .one();
    }

    @Override
    public List<LastLocationData> queryLastLocationDataByImeis(List<String> imeis) {
        if (imeis == null || imeis.size() == 0) {
            return null;
        }
        return new LambdaQueryChainWrapper<>(lastLocationDataMapper)
                .in(LastLocationData::getDeviceImei, imeis)
                .list();
    }

    @Override
    public List<Map<String, Object>> getClassesLocationListByClassOrImei(Integer classId, String imei, String orgCode) {
        return lastLocationDataMapper.getClassesLocationListByClassOrImei(classId, imei, orgCode);
    }

    @Override
    public List<Map<String, Object>> getLastLocationByStudentIds(List<Integer> stuIdList) {
        if (stuIdList == null || stuIdList.size() == 0) {
            return null;
        }
        return lastLocationDataMapper.getLastLocationByStudentIds(stuIdList);
    }

    @Override
    public List<Map<String, Object>> queryH5LastLocationDataByDeviceIds(List<Integer> queryList, Integer userId) {
        if (queryList == null || queryList.size() == 0||userId==null) {
            return null;
        }
        return lastLocationDataMapper.queryH5LastLocationDataByDeviceIds(queryList, userId);
    }
}
